//
//  YRJDiscountZoneForAllViewController.m
//  YanRenjie
//
//  Created by dllo on 15/10/30.
//  Copyright (c) 2015年 yanrenjie. All rights reserved.
//

#import "YRJDiscountZoneForAllViewController.h"
#import "YRJTripHeader.h"

@interface YRJDiscountZoneForAllViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, retain)UITableView *tableView;
@property(nonatomic, retain)NSMutableArray *allDiscountArr;
@end

NSInteger discountZoneForAllPageIndex = 1;
@implementation YRJDiscountZoneForAllViewController
- (void)dealloc {
    [_tableView release];
    [_allDiscountArr release];
    
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    [_tableView release];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(pullDownToRefresh)];
    self.tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        discountZoneForAllPageIndex++;
        [self pullUpToLoading];
    }];
    [self createData];
}

- (void)createData {
    NSString *strURL = @"http://open.qyer.com/lastminute/app_lastminute_list";
    NSURL *url = [NSURL URLWithString:strURL];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"post"];
    NSString *bodyStr = [NSString stringWithFormat:@"%@%ld%@", @"client_id=qyer_android&client_secret=9fcaae8aefc4f9ac4915&v=1&track_deviceid=867739013144832&track_app_version=6.8.1&track_app_channel=hiapk&track_device_info=bbk77_cu_ics2&track_os=Android4.0.4&app_installtime=1445390305001&lat=38.883438&lon=121.544453&page_size=10&page=", (long)discountZoneForAllPageIndex, @"&product_type=0&continent_id=0&country_id=0&city_id=0"];
    NSData *dataBody = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];
    [request setHTTPBody:dataBody];
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
    self.allDiscountArr = [YRJDiscountZoneForAllModel baseModelByArr:dic[@"data"][@"lastminutes"]];
//    for (YRJDiscountZoneForAllModel *tempModel in self.allDiscountArr) {
//        NSLog(@"%@", tempModel.title);
//    }
}

#pragma mark - 上拉加载, 下拉刷新的方法
- (void)pullDownToRefresh {
    [self.allDiscountArr removeAllObjects];
    [self createData];
}

- (void)pullUpToLoading {
    NSString *strURL = @"http://open.qyer.com/lastminute/app_lastminute_list";
    NSURL *url = [NSURL URLWithString:strURL];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"post"];
    NSString *bodyStr = [NSString stringWithFormat:@"%@%ld%@", @"client_id=qyer_android&client_secret=9fcaae8aefc4f9ac4915&v=1&track_deviceid=867739013144832&track_app_version=6.8.1&track_app_channel=hiapk&track_device_info=bbk77_cu_ics2&track_os=Android4.0.4&app_installtime=1445390305001&lat=38.883438&lon=121.544453&page_size=10&page=", (long)discountZoneForAllPageIndex, @"&product_type=0&continent_id=0&country_id=0&city_id=0"];
    NSData *dataBody = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];
    [request setHTTPBody:dataBody];
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
    NSArray *tempArr = [YRJDiscountZoneForAllModel baseModelByArr:dic[@"data"][@"lastminutes"]];
    for (YRJDiscountZoneForAllModel *tempModel in tempArr) {
        [self.allDiscountArr addObject:tempModel];
    }
    [self.tableView reloadData];
    [self.tableView.header endRefreshing];
    [self.tableView.footer endRefreshing];
}


#pragma mark - tableView 的协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.allDiscountArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *reuse = @"reuse";
    YRJDiscountZoneForAllTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell = [[YRJDiscountZoneForAllTableViewCell alloc] initWithStyle:0 reuseIdentifier:reuse];
    }
    if (self.allDiscountArr.count != 0) {
        [cell.picImageView sd_setImageWithURL:[NSURL URLWithString:[self.allDiscountArr[indexPath.row] pic]] placeholderImage:[UIImage imageNamed:@"placeHold.png"]];
        cell.titleLabel.text = [self.allDiscountArr[indexPath.row] title];
        cell.departureTimeLabel.text = [NSString stringWithFormat:@"%@%@", @"出发时间:", [self.allDiscountArr[indexPath.row] departureTime]];
        cell.lastminute_desLabel.text = [self.allDiscountArr[indexPath.row] lastminute_des];
        cell.priceLabel.text = [[[self.allDiscountArr[indexPath.row] price] stringByReplacingOccurrencesOfString:@"<em>" withString:@""] stringByReplacingOccurrencesOfString:@"</em>" withString:@""];
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return (140.0 / 667) * HEIGHT;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    YRJDiscountZoneForAllWebViewController *discountZoneForAllWebVC = [[YRJDiscountZoneForAllWebViewController alloc] init];
    [self.navigationController pushViewController:discountZoneForAllWebVC animated:YES];
    discountZoneForAllWebVC.string = (NSString *)[self.allDiscountArr[indexPath.row] url];
    [discountZoneForAllWebVC release];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
